﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Continuum.Classes;

namespace Continuum
{
    public partial class DownloadFundPerformance : Form
    {
        public DownloadFundPerformance()
        {
            InitializeComponent();
        }

        private void DownloadFundPerformance_Load(object sender, EventArgs e)
        {
            if (MainForm.market.MarketFunds.Count == 0) {
                MessageBox.Show("You need to add some market forms in the EditMarketFund screen first!", "Unable to download data", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                return;
            }

            ddlFunds.DataSource = MainForm.market.MarketFunds;
        }

        private void ddlFunds_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (ddlFunds.SelectedItem == null) return;

            MarketFund marketfund = (MarketFund) ddlFunds.SelectedItem;

            dtpHistoricalStart.Value = marketfund.InceptionDate;
            dtpHistoricalEnd.Value = DateTime.Now.Date;
            numSampleRate.Value = 7;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btnDownload_Click(object sender, EventArgs e)
        {
            if (ddlFunds.SelectedItem == null) return;

            MarketFund marketfund = (MarketFund)ddlFunds.SelectedItem;

            if (MainForm.market.DownloadHistory(marketfund, (int) numSampleRate.Value, dtpHistoricalStart.Value, dtpHistoricalEnd.Value))
                MessageBox.Show("Market Data Downloaded Successfully for " + marketfund.Name, "Download Market Data", MessageBoxButtons.OK, MessageBoxIcon.Information);
            else
                MessageBox.Show("There was an error downloading Market Data for " + marketfund.Name, "Download Market Data", MessageBoxButtons.OK, MessageBoxIcon.Error);

            Close();
        }

        private void lnkOnlineHelp_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            System.Diagnostics.Process proc = new System.Diagnostics.Process();
            proc.StartInfo.UseShellExecute = true;
            proc.StartInfo.FileName = "http://project-continuum.com/OnlineHelp/DownloadFundPerformance.aspx";
            proc.Start();

        }
    }
}
